package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    @DubboReference
    private HouseService houseService;
    @DubboReference
    private DictService dictService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseUserService houseUserService;


    @RequestMapping
    public String house(HttpServletRequest request, Map map){
        Map<String, Object> filters = getFilters(request);
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page", page);
        map.put("filters", filters);
        getValues(map);

        return "house/index";
    }

    @RequestMapping("/create")
     public String create(Map map){
        getValues(map);
        return "house/create";
    }
    @RequestMapping("/save")
    public String insert(House house){
        houseService.insert(house);
        return "common/success";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map){
        House house = houseService.getById(id);
        map.put("house",house);
        return "house/edit";
    }
    @RequestMapping("/update")
    public String update(House house){
        houseService.update(house);
        return "common/success";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id, Map map){
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable Integer status){
        houseService.publish(id,status);
        return "redirect:/house";
    }
    @RequestMapping("/show/{id}")
    public String show(@PathVariable Long id, Map map){
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        map.put("house",house);
        map.put("community", community);
        List<HouseImage> houseImage1List = houseImageService.getByHouseId(id,1);
        List<HouseImage> houseImage2List = houseImageService.getByHouseId(id,2);
        map.put("houseImage1List", houseImage1List);
        map.put("houseImage2List", houseImage2List);
        List<HouseBroker> houseBrokerList = houseBrokerService.getByHouseId(id);
        map.put("houseBrokerList", houseBrokerList);
        List<HouseUser> houseUserList = houseUserService.findUserByHouseId(id);
        map.put("houseUserList", houseUserList);
        return "house/show";
    }



    public void getValues(Map map){
        List<Community> communityList = communityService.getAll();
        List<Dict> houseTypeList = dictService.findDictByCode("houseType");
        List<Dict> floorList = dictService.findDictByCode("floor");
        List<Dict> buildStructureList = dictService.findDictByCode("buildStructure");
        List<Dict> directionList = dictService.findDictByCode("decoration");
        List<Dict> decorationList = dictService.findDictByCode("direction");
        List<Dict> houseUseList = dictService.findDictByCode("houseUse");
        map.put("communityList", communityList);
        map.put("houseTypeList", houseTypeList);
        map.put("floorList", floorList);
        map.put("buildStructureList", buildStructureList);
        map.put("directionList", directionList);
        map.put("decorationList", decorationList);
        map.put("houseUseList", houseUseList);
    }
}
